package net.xdclass.controller;


import net.xdclass.controller.request.LinkGroupAddRequest;
import net.xdclass.controller.request.LinkGroupUpdateRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.service.LinkGroupService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王展
 * @since 2024-07-21
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {
    @Autowired
    private LinkGroupService linkGroupService;
    @PostMapping("add")
    public JsonData add(@RequestBody LinkGroupAddRequest linkGroupAddRequest){
        int rows = linkGroupService.add(linkGroupAddRequest);
        return rows == 1 ? JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }

    @DeleteMapping("/del/{group_id}")
    public JsonData del(@PathVariable("group_id") String groupId){
        //boolean isDel = false;
        boolean isDel = linkGroupService.del(groupId);
        return isDel ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.GROUP_DEL_ERROR);
    }

    @GetMapping("/detail/{group_id}")
    public JsonData detail(@PathVariable("group_id") Long groupId){

        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }

    @GetMapping("/list")
    public JsonData listAll(){

        List<LinkGroupVO> linkGroupList = linkGroupService.listAll();
        return JsonData.buildSuccess(linkGroupList);
    }

    @PutMapping("/update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest request){

        int rows = linkGroupService.update(request);
        return rows == 1 ? JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_UPDATE_ERROR);
    }


}

